<html>
<head><meta charset="utf-8"><title>Map/Mutate error in Compiler Session Diagnostics · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html">Map/Mutate error in Compiler Session Diagnostics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248254289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248254289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248254289">(Aug 03 2021 at 17:43)</a>:</h4>
<p>Hi all, </p>
<p>I am working under project prusti, which is designed to be a rust compiler plugin. In my feature I will need to access the error recorded in compiler session and map/mutate it to be our own error type instead of calling <code>abort_if_error()</code> API, is there any existing functions I can call to get a mutable reference of error?</p>



<a name="248266541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248266541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248266541">(Aug 03 2021 at 19:17)</a>:</h4>
<p>By the time you would fall <code>abort_if_error()</code>, the errors have already been written to stderr and as such can't be changed or AFAIK read back.</p>



<a name="248268242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248268242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248268242">(Aug 03 2021 at 19:29)</a>:</h4>
<p>Then is there a way to catch the error earlier? Before it's written into stderr perhaps?</p>



<a name="248268462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248268462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248268462">(Aug 03 2021 at 19:30)</a>:</h4>
<p>Or is there a way that I can register a callback to handle the error differently somewhere?</p>



<a name="248275196"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248275196" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248275196">(Aug 03 2021 at 20:21)</a>:</h4>
<p>When using a custom driver, you can set the <code>diagnostic_output</code> field of <code>rustc_interface::interface::Config</code> to an arbitrary <code>dyn Write</code> from the <code>rustc_driver::Callbacks::config</code> implementation or you can pass it to <code>rustc_driver::RunCompiler::set_emitter</code>.</p>



<a name="248283792"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248283792" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248283792">(Aug 03 2021 at 21:30)</a>:</h4>
<p>Would that prevent the compiler from emitting the error if <code>abort_if_error()</code> is called?<br>
Also if I do that I suppose all of the errors produced in compilation will be directed to the <code>dyn Write</code> I provide and thus no error will ever be reported through <code>stderr</code>?</p>



<a name="248359078"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248359078" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248359078">(Aug 04 2021 at 14:39)</a>:</h4>
<blockquote>
<p>Would that prevent the compiler from emitting the error if abort_if_error() is called?</p>
</blockquote>
<p>No, the error is already emitted long before you have the chance to call <code>abort_if_error()</code>.</p>
<blockquote>
<p>Also if I do that I suppose all of the errors produced in compilation will be directed to the dyn Write I provide and thus no error will ever be reported through stderr?</p>
</blockquote>
<p>Correct</p>



<a name="248374825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248374825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248374825">(Aug 04 2021 at 16:38)</a>:</h4>
<p>Thanks for the answer. We can indeed set <code>diagnostic_output</code>, but it seems this is essentially string-based. Is there also a way to provide something like a custom <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/emitter/trait.Emitter.html"><code>emitter</code></a>?</p>



<a name="248377416"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248377416" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248377416">(Aug 04 2021 at 16:59)</a>:</h4>
<p>Doesn't seem so. <code>build_session()</code> which is responsible for creating the <code>Session</code>, doesn't accept one. It calls <code>default_emitter()</code>.</p>



<a name="248395129"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248395129" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aurel Bílý <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248395129">(Aug 04 2021 at 19:16)</a>:</h4>
<p>Would you say that a PR to add a new variant to <code>DiagnosticOutput</code> would work?</p>



<a name="248405400"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248405400" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248405400">(Aug 04 2021 at 20:35)</a>:</h4>
<p>Maybe? You could try if you need to.</p>



<a name="248524347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248524347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248524347">(Aug 05 2021 at 19:27)</a>:</h4>
<p>Assume that we provide a custom emitter, is it possible to resolve to the original <code>ast</code> item where the error is generated?</p>



<a name="248524818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248524818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248524818">(Aug 05 2021 at 19:31)</a>:</h4>
<p>No, diagnostics only carry a span, which does not need to have any correspondence with any AST item.</p>



<a name="248525122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248525122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248525122">(Aug 05 2021 at 19:34)</a>:</h4>
<p>Yeah but in the emitter, we want to differentiate the error that we produce from the error user caused, so I was wondering if there's a robust way to do that. Our proc-macro expansion will annotate the item with our attribute, so if I can get the ast item then it's easy for me to see what kind of error it is</p>



<a name="248526057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248526057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248526057">(Aug 05 2021 at 19:41)</a>:</h4>
<p>You can check the expansion data of the span. I don't know how much information that has for proc-macros, but you may be able to distinguish your proc-macro from other (proc-)macros.</p>



<a name="248564640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Map/Mutate%20error%20in%20Compiler%20Session%20Diagnostics/near/248564640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jerry Liu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Map.2FMutate.20error.20in.20Compiler.20Session.20Diagnostics.html#248564640">(Aug 06 2021 at 03:19)</a>:</h4>
<p>Oh another small question, I was trying to provide a custom emitter with the same error output format as the default one, but if I use a <code>EmitterWriter</code>: <a href="https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/emitter/struct.EmitterWriter.html">https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/emitter/struct.EmitterWriter.html</a>. I lost the snippet. If I use <code>AnnotateSnippetEmitterWriter</code>: <a href="https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/annotate_snippet_emitter_writer/struct.AnnotateSnippetEmitterWriter.html">https://doc.rust-lang.org/beta/nightly-rustc/rustc_errors/annotate_snippet_emitter_writer/struct.AnnotateSnippetEmitterWriter.html</a>, I can't find anyway to pass color into it. The default one seems to have both color and snippet working, how can I do that?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>